package com.lannuokeji.config;

import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ibeetl.starter.BeetlTemplateCustomize;
import com.lannuokeji.utils.tags.array.CurrentFunction;
import com.lannuokeji.utils.tags.array.HasPermission;
import com.lannuokeji.utils.tags.array.SysParamTag;
import com.lannuokeji.utils.tags.map.DataListTag;
import com.lannuokeji.utils.tags.map.DataPageTag;
import com.lannuokeji.utils.tags.map.DataRowTag;
import com.lannuokeji.utils.tags.map.DicListTag;
import com.lannuokeji.utils.tags.map.UserTag;

@Configuration
public class IBeetlConfig {

	@Bean
	public BeetlTemplateCustomize beetlTemplateCustomize() {
		return new BeetlTemplateCustomize() {

			@Override
			public void customize(GroupTemplate gt) {
				// 根据gt可以添加扩展函数，格式化函数，共享变量等，
				gt.registerTag(DataPageTag.tagName, DataPageTag.class);
				gt.registerTag(DicListTag.tagName, DicListTag.class);
				gt.registerTag(DataRowTag.tagName, DataRowTag.class);
				gt.registerTag(DataListTag.tagName, DataListTag.class);
				gt.registerTag(UserTag.tagName, UserTag.class);

				gt.registerFunction(SysParamTag.tagName, new SysParamTag());
				gt.registerFunction(HasPermission.functionName, new HasPermission());
				gt.registerFunction(CurrentFunction.functionName, new CurrentFunction());
			}
		};
	}

	@Bean(name = "beetlConfig")
	public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
		BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
		// 获取Spring Boot 的ClassLoader
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		if (loader == null) {
			loader = IBeetlConfig.class.getClassLoader();
		}
		// beetlGroupUtilConfiguration.setConfigProperties(extProperties);//额外的配置，可以覆盖默认配置，一般不需要
		ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, "templates");
		beetlGroupUtilConfiguration.setResourceLoader(cploder);
		beetlGroupUtilConfiguration.init();
		// 如果使用了优化编译器，涉及到字节码操作，需要添加ClassLoader
		beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
		
		GroupTemplate gt = beetlGroupUtilConfiguration.getGroupTemplate();
		gt.registerTag(DataPageTag.tagName, DataPageTag.class);
		gt.registerTag(DicListTag.tagName, DicListTag.class);
		gt.registerTag(DataRowTag.tagName, DataRowTag.class);
		gt.registerTag(DataListTag.tagName, DataListTag.class);
		gt.registerTag(UserTag.tagName, UserTag.class);

		gt.registerFunction(SysParamTag.tagName, new SysParamTag());
		gt.registerFunction(HasPermission.functionName, new HasPermission());
		gt.registerFunction(CurrentFunction.functionName, new CurrentFunction());
		
		return beetlGroupUtilConfiguration;

	}

	@Bean(name = "beetlViewResolver")
	public BeetlSpringViewResolver getBeetlSpringViewResolver(
			@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
		beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
//		beetlSpringViewResolver.setOrder(0);
		beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
		return beetlSpringViewResolver;
	}

}
